<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<link rel="stylesheet"  href="<c:url value='/resource/css/zTreeStyle/zTreeStyle.css'/>" type="text/css"></link>
<script type="text/javascript" src="<c:url value='/resource/js/jquery-1.7.2.min.js'/>"></script>
<script type="text/javascript" src="<c:url value='/resource/js/jquery.ztree.core-3.2.js'/>"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/resource/js/jquery.ztree.excheck-3.2.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/resource/js/jquery.ztree.exedit-3.2.js"></script>
<script type="text/javascript" src="<c:url value='/resource/js/common.js'/>"></script>
<script type="text/javascript">
	var setting = {
		async : {
			enable : true,
			url : "<c:url value='/dept/getAllDept'/>",
			dataType : "json",
			type : 'get',
			autoParam : [ "deptId" ],
			dataFilter : function(treeId, parentNode, childNodes) {
				return childNodes.data
			}
		},
		callback : {
			onClick : zTreeOnClick,
			beforeExpand: beforeExpand,
			onExpand: onExpand,
			beforeDrag: beforeDrag,
			beforeDrop: beforeDrop
		},
		data : {
			key:{
				name:"alias"
			},
			simpleData : {
				enable : true,
				idKey : "deptId",
				pIdKey : "parentId",
				rootPId : 0
			}
		},
		edit:{
			enable:true,
			showRemoveBtn:false,
			showRenameBtn:false
		}
	};
	
	$(function() {
		$.fn.zTree.init($("#treeDemo"), setting);
	});
	
	//拖拽事件
	function beforeDrag(treeId, treeNodes) {
		for (var i=0,l=treeNodes.length; i<l; i++) {
			if (treeNodes[i].drag === false) {
				return false;
			}
		}
		return true;
	}
	function beforeDrop(treeId, treeNodes, targetNode, moveType) {
		//判断节点是否可以拖拽
		var drogAble = targetNode ? targetNode.drop !== false : true;
		if(drogAble){
			if(treeNodes[0].parentId!=targetNode.parentId){
				art.dialog.alert("必须上级部门一致才能拖动");
				return false;
			}
			if(moveType=="inner"){
				art.dialog.alert("不可拖动到非同级部门");
				return false;
			}
			var flag = false;
			//后台修改
				$.ajax({
					type:"POST",
					url:"<%=request.getContextPath()%>/dept/changeorder.json",
					async:false,
					data:{
						'deptId':treeNodes[0].deptId,
						'orderNum':treeNodes[0].orderNum,
						'targetOrder':targetNode.orderNum
					},
					success:function(data){
						flag = true;
					}
				});
			return flag;
		}else{
			return false;
		}
	}
	/**同时只能展开一个节点，减少树的长度 start*/
	var curExpandNode = null;
	function beforeExpand(treeId, treeNode) {
		var pNode = curExpandNode ? curExpandNode.getParentNode():null;
		var treeNodeP = treeNode.parentTId ? treeNode.getParentNode():null;
		var zTree = $.fn.zTree.getZTreeObj("treeDemo");
		for(var i=0, l=!treeNodeP ? 0:treeNodeP.children.length; i<l; i++ ) {
			if (treeNode !== treeNodeP.children[i]) {
				zTree.expandNode(treeNodeP.children[i], false);
			}
		}
		while (pNode) {
			if (pNode === treeNode) {
				break;
			}
			pNode = pNode.getParentNode();
		}
		if (!pNode) {
			singlePath(treeNode);
		}
	}
	
	function singlePath(newNode) {
		if (newNode === curExpandNode) return;
		if (curExpandNode && curExpandNode.open==true) {
			var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			if (newNode.parentTId === curExpandNode.parentTId) {
				zTree.expandNode(curExpandNode, false);
			} else {
				var newParents = [];
				while (newNode) {
					newNode = newNode.getParentNode();
					if (newNode === curExpandNode) {
						newParents = null;
						break;
					} else if (newNode) {
						newParents.push(newNode);
					}
				}
				if (newParents!=null) {
					var oldNode = curExpandNode;
					var oldParents = [];
					while (oldNode) {
						oldNode = oldNode.getParentNode();
						if (oldNode) {
							oldParents.push(oldNode);
						}
					}
					if (newParents.length>0) {
						for (var i = Math.min(newParents.length, oldParents.length)-1; i>=0; i--) {
							if (newParents[i] !== oldParents[i]) {
								zTree.expandNode(oldParents[i], false);
								break;
							}
						}
					} else {
						zTree.expandNode(oldParents[oldParents.length-1], false);
					}
				}
			}
		}
		curExpandNode = newNode;
	}

	function onExpand(event, treeId, treeNode) {
		curExpandNode = treeNode;
	}
	/**end*/
</script>
<div id="tree_wrap_div" class="zTreeDemoBackground left" style="float:left;width:150px;height:500px;padding:0px;overflow-x:hidden;overflow-y:auto;background-color:#f2f2f2;scrollbar-track-color:#fff;scrollbar-3dlight-color:#BDCFE4;scrollbar-arrow-color:#BDCFE4;scrollbar-face-color:#fff;scrollbar-shadow-color:#fff;scrollbar-darkshadow-color:#BDCFE4;">
	<ul id="treeDemo" class="ztree" style="width:150px;"></ul>
</div>
